package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.GetPaymentsPagingAction;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.server.data.WebetekUser;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.shared.PagingResult;
import com.aptemo.webetek.shared.PaymentBean;

public class PaymentsPagingHandler
		extends ActionHandler<GetPaymentsPagingAction, PagingResult<PaymentBean>>
{

	public PaymentsPagingHandler(DataStoreAPI service)
	{
		super(service);
	}

	public Class<? extends Action<?>> getActionType()
	{
		return GetPaymentsPagingAction.class;
	}

	@Override
	public PagingResult<PaymentBean> invoke(GetPaymentsPagingAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();

		if (!utils.isTeamMember(action.getTeamId()))
		{
			throw new BackendException(EErrorCodes.NOT_TEAM_MEMBER);
		}
		
		WebetekUser user = utils.getCurrentUser(action.getTeamId());
		
		String userId = null;
		if (user != null)
		{
			userId = user.getSid();			
		}

		PagingResult<PaymentBean> result = dataService.getPayments(userId, action.getType(), action.getOrder(), action.getCursor(), action.getPageSize());
		return result;
	}
}
